package com.smartmedical.doctor.friend.service.impl;

import com.smartmedical.common.core.domain.HealthR;
import com.smartmedical.common.security.utils.SecurityUtils;
import com.smartmedical.doctor.friend.domain.vo.PatientCircleVo;
import com.smartmedical.doctor.friend.domain.vo.SickCircleByKeyWordVo;
import com.smartmedical.doctor.friend.domain.vo.SickCircleVo;
import com.smartmedical.doctor.friend.mapper.PatientCircleMapper;
import com.smartmedical.doctor.friend.service.PatientCircleService;
import com.smartmedical.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author：闫晓琪
 * @Filename：PatientCircleServiceImpl
 * @Package：com.smartmedical.dcotor.firend.service.impl
 * @Project：smart-medical
 * @name：PatientCircleServiceImpl
 * @Date：2024/1/12 21:34
 */

@Service
public class PatientCircleServiceImpl implements PatientCircleService {

    @Autowired
    private PatientCircleMapper patientCircleMapper;
    @Override
    public  HealthR<List<PatientCircleVo>> findSickCircleList(Integer departmentId) {
        List<PatientCircleVo> patientCircleVoList = patientCircleMapper.findSickCircleList(departmentId);
        if (patientCircleVoList == null) {
            return HealthR.fail("病友圈列表查询失败");
        }
        return HealthR.ok(patientCircleVoList,"病友圈列表查询成功");
    }

    @Override
    public HealthR<SickCircleVo> findSickCircleInfo(Integer sickCircleId) {
        SickCircleVo result=patientCircleMapper.findSickCircleInfo(sickCircleId);
        if (result == null) {
            return HealthR.fail("病友圈详情查询失败");
        }
        return HealthR.ok(result,"病友圈详情查询成功");
    }



    @Override
    public HealthR<List<SickCircleByKeyWordVo>> searchSickCircle(String keyWord) {
        //根据关键词查询病友圈
        List<SickCircleByKeyWordVo> result=patientCircleMapper.searchSickCircle(keyWord);
        if (result == null) {
            return HealthR.fail("关键字病友圈列表查询失败");
        }
        return HealthR.ok(result,"关键字病友圈列表查询成功");

    }

    @Override
    public HealthR publishComment(Integer sickCircleId, String content) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userid = loginUser.getUserid();
        String email = loginUser.getUsername();     //极光IM的账号
        String sel = patientCircleMapper.selectDoctorByUserName(email);
        Integer whetherDoctor =0;
        if (sel !=null ) {
            whetherDoctor = 1;
        }
        //获取当前时间
        Date date = new Date();

        Integer result=patientCircleMapper.publishComment(whetherDoctor,userid,sickCircleId,content,date);
        if (result > 0) {
            return HealthR.ok("评论成功");
        }
        return HealthR.fail("评论失败");
    }
}
